﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using SimpleFileManager.Manager;
using System.Runtime.Serialization.Formatters.Binary;

namespace SimpleFileManager
{
    public partial class FormMain : Form
    {
        FormMainPresenter _presenter;

        public FormMain(FormMainPresenter presenter)
        {
            _presenter = presenter;
            _presenter.View = this;
            InitializeComponent();
            fileViewActive.Presenter = presenter;
            fileViewInactive.Presenter = presenter;
            try
            {
                _presenter.LoadState("state.bin");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public void ChangeViews()
        {
            FileView tmp = fileViewActive;
            fileViewActive = fileViewInactive;
            fileViewInactive = tmp;            
        }

        private void InitiateChangeViews()
        {
            ChangeViews();
            _presenter.ChangePanels();
        }

        private void formNameInput_NewFolder(object sender, ConfirmEventArgs e)
        {
            try
            {
                _presenter.Create(FilePanelItemFactory.CreateFolder(e.Name));
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void formNameInput_Rename(object sender, ConfirmEventArgs e)
        {
            try
            {
                _presenter.Rename(fileViewActive.SelectedItems, e.Name);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void formNameInput_ApplyFileFilter(object sender, ConfirmEventArgs e)
        {
            try
            {
                _presenter.ApplyFileFilter(e.Name);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void UncheckViewButtons()
        {
            largeToolStripMenuItem.Checked = false;
            smallIconsToolStripMenuItem.Checked = false;
            listToolStripMenuItem.Checked = false;
            detailsToolStripMenuItem.Checked = false;
        }

        private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileViewActive.View = View.LargeIcon;
            fileViewInactive.View = View.LargeIcon;
            UncheckViewButtons();
            largeToolStripMenuItem.Checked = true;
        }

        private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileViewActive.View = View.SmallIcon;
            fileViewInactive.View = View.SmallIcon;
            UncheckViewButtons();
            smallIconsToolStripMenuItem.Checked = true;
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileViewActive.View = View.List;
            fileViewInactive.View = View.List;
            UncheckViewButtons();
            listToolStripMenuItem.Checked = true;
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileViewActive.View = View.Details;
            fileViewInactive.View = View.Details;
            UncheckViewButtons();
            detailsToolStripMenuItem.Checked = true;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            _presenter.Refresh();
        }

        private void fileView_Enter(object sender, EventArgs e)
        {
            if (!sender.Equals(fileViewActive))
            {
                InitiateChangeViews();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            _presenter.GoBack();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            _presenter.GoForward();
        }

        private void btnNewFolder_Click(object sender, EventArgs e)
        {
            FormNameInput formNameInput = new FormNameInput();
            formNameInput.OnConfirm += new ConfirmEventHandler(formNameInput_NewFolder);
            formNameInput.ShowDialog();
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (fileViewActive.SelectedItems.Count == 1)
            {
                FormNameInput formNameInput = new FormNameInput(fileViewActive.SelectedItems.Name);
                formNameInput.OnConfirm += new ConfirmEventHandler(formNameInput_Rename);
                formNameInput.ShowDialog();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Deleting files", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                _presenter.Delete(fileViewActive.SelectedItems);
            }
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            try
            {
                _presenter.Move(fileViewActive.SelectedItems);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            try
            {
                _presenter.Copy(fileViewActive.SelectedItems);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            // обработка нажатия клавиши backspace
            switch ((byte)e.KeyCode)
            {
                case 8: _presenter.GoToParentDirectory(); break;
                case 9: InitiateChangeViews(); break;
            }
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                _presenter.SaveState("state.bin");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void filterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNameInput formNameInput = new FormNameInput();
            formNameInput.OnConfirm += new ConfirmEventHandler(formNameInput_ApplyFileFilter);
            formNameInput.ShowDialog();
        }

        public void ShowFolderContent(IFolderIterator di,bool isActive)
        {
            if (isActive)
            {
                fileViewActive.ShowFolderContent(di);
            }
            else
            {
                fileViewInactive.ShowFolderContent(di);
            }
        }
    }
}
